<?php
/**
 * zOOm Media Gallery! - a multi-gallery component 
 * 
 * @package zmg
 * @version $Revision$
 * @author Mike de Boer <mike AT zoomfactory.org>
 * @copyright Copyright &copy; 2007, Mike de Boer. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GPL
 */

defined('_ZMG_EXEC') or die('Restricted access');

class zmgDocumentTool {
    /**
     * Extract the raw text from a PDF document (format: '{filename}.txt') with the Xpdf library (pdftotext)
     *
     * @param string $file
     * @param string $filename
     * @return boolean
     */
    function process(&$medium, &$gallery) {
        // this function will contain the algorithm to index a document (like a pdf)...
        // Method: use PDFtoText to create a plain ASCII text-file, which can be easily
        //         searched through. The text-file will be placed into the same dir as the
        //         original pdf.
        // Note: support for MS Word, Excel and Powerpoint indexing will be added later.
        $file = $medium->getAbsPath();

        $filename    = ereg_replace("(.*)\.([^\.]*)$", "\\1", $medium->filename).".txt";
        $target_file = zmgEnv::getRootPath() . DS
         . zmgFactory::getConfig()->get('filesystem/mediapath') . $gallery->dir
         . DS . $filename;

        $cmd = zmgDocumentTool::getPath() . "pdftotext \"$file\" \"$target_file\"";
        $output = $retval = null;
        exec($cmd, $output, $retval);
        if ($retval) {
            return zmgToolboxPlugin::registerError($file, 'Xpdf: Could not index document: ' . $output);
        }
        return true;
    }
    /**
     * Perform a search with a given search-string in PDF-index files generated by zOOm.
     *
     * @param string $file
     * @param string $searchText
     * @return boolean
     */
    function search($file, $searchText) {
        //FIXME: update this stuff to the new API

        global $mosConfig_absolute_path, $zoom;
        if (empty($file->_filename)) {
            $file->getInfo();
        }
        $source = $mosConfig_absolute_path."/".$zoom->_CONFIG['imagepath'].$file->getDir()."/".ereg_replace("(.*)\.([^\.]*)$", "\\1", $file->_filename).".txt";
        if (!$zoom->platform->is_file($source)) {
            return zmgToolboxPlugin::registerError($file, 'Xpdf: File is not indexed yet.');
        }
        $txt = strtolower(file_get_contents($source));
        if (preg_match("/$searchText/", $txt)) {
            unset($txt);
            return true;
        }
        unset($txt);
        return zmgToolboxPlugin::registerError($file, 'Search: Term not found in this document.');
    }
    
    function getPath() {
         $config = & zmgFactory::getConfig();
         
         $path     = trim($config->get('plugins/toolbox/pdftotext/path'));
         $override = intval($config->get('plugins/toolbox/pdftotext/override'));
         
         if ($path == "auto") {
            $path = zmgDocumentTool::detectPath();
         }
         
         return $path;
    }
    
    function detectPath() {
        $path = "";
        if (file_exists('/usr/bin/pdftotext') && is_executable('/usr/bin/pdftotext')) {
            $path = "/usr/bin/"; //Debian systems
        }
        return $path;
    }

    /**
     * Detect if Xpdf is available on the system.
     *
     * @return void
     */
    function autoDetect() {
        static $output, $status;
        $bar = @exec(zmgDocumentTool::detectPath() . 'pdftotext -v', $output, $status);
        
        $res = false;
        if (!empty($output[0])) {
            if (preg_match("/pdftotext/i", $output[0], $matches)) {
                zmgToolboxPlugin::registerError(T_('Xpdf (or pdftotext)'), T_('is available.'));
                $res = true;
            }
        }
        if (!$res) {
            zmgToolboxPlugin::registerError(T_('Xpdf (or pdftotext)'), T_('could not be detected on your system.'));
        }
        unset($output, $status);
    }
}
?>
